package com.jsoft.service.impl;

import com.jsoft.dao.ContractDao;
import com.jsoft.dao.ContractInfoVoDao;
import com.jsoft.dao.ContractVoDao;
import com.jsoft.dao.impl.ContractDaoImpl;
import com.jsoft.dao.impl.ContractInfoVoDaoImpl;
import com.jsoft.dao.impl.ContractVoDaoImpl;
import com.jsoft.entiy.Check;
import com.jsoft.entiy.Contract;
import com.jsoft.entiy.vo.ContractInfoVo;
import com.jsoft.entiy.vo.ContractVo;
import com.jsoft.service.ContractService;
import com.jsoft.util.CastPageUtil;
import com.jsoft.util.NowTime;

import java.util.List;

/**
 * @program: CPM
 * @description:
 * @author: beibei
 * @create: 2022-09-07 15:52
 */
public class ContractServiceImpl implements ContractService {

    private ContractDao contractDao = new ContractDaoImpl();
    private ContractVoDao contractVoDao = new ContractVoDaoImpl();
    private ContractInfoVoDao contractInfoVo = new ContractInfoVoDaoImpl();
    @Override
    public List<Contract> getAll() {
        List<Contract> contracts = contractDao.getAll();
        return contracts;
    }

    @Override
    public Integer getUnpaidContractInfo() {
        List<ContractVo> list = contractVoDao.getUnpaidContractInfo();
        CastPageUtil castPageUtil = new CastPageUtil(list,1);
        return castPageUtil.getTotalNum();
    }

    @Override
    public List<ContractVo> getUnpaidContractInfo(String nowPage) {
        List<ContractVo> list = contractVoDao.getUnpaidContractInfo();
        CastPageUtil castPageUtil = new CastPageUtil(list, Integer.parseInt(nowPage));
        List dataList = castPageUtil.getCurrentDataList();
        return dataList;
    }

    @Override
    public Integer getPaidContractInfo() {
        List<ContractVo> list = contractVoDao.getPaidContractInfo();
        CastPageUtil castPageUtil = new CastPageUtil(list,1);
        return castPageUtil.getTotalNum();
    }

    @Override
    public List<ContractVo> getPaidContractInfo(String nowPage) {
        List<ContractVo> list = contractVoDao.getPaidContractInfo();
        CastPageUtil castPageUtil = new CastPageUtil(list, Integer.parseInt(nowPage));
        List dataList = castPageUtil.getCurrentDataList();
        return dataList;
    }

    @Override
    public Integer getRenewalContractInfo() {
        List<ContractVo> list = contractVoDao.getRenewalContractInfo();
        CastPageUtil castPageUtil = new CastPageUtil(list,1);
        return castPageUtil.getTotalNum();
    }

    @Override
    public List<ContractVo> getRenewalContractInfo(String nowPage) {
        List<ContractVo> list = contractVoDao.getRenewalContractInfo();
        CastPageUtil castPageUtil = new CastPageUtil(list, Integer.parseInt(nowPage));
        List dataList = castPageUtil.getCurrentDataList();
        return dataList;
    }

    @Override
    public ContractInfoVo getInfoById(Integer id) {
        ContractInfoVo infoVoById = contractInfoVo.getInfoVoById(id);
        return infoVoById;
    }

    @Override
    public ContractInfoVo getInfoVoByName(String name) {
        return contractInfoVo.getInfoVoByName(name);
    }

    @Override
    public Integer save(Contract contract) {
        //设置创建和修改时间
        contract.setGmtCreate(NowTime.getNowTime());
        contract.setGmtModified(NowTime.getNowTime());
        return contractDao.save(contract);
    }

    @Override
    public List<ContractVo> checkInfo(Check check, String nowPage) {
        if (check.getName().length()==0){
            check.setName(null);
        }
        if (check.getType().length()==0){
            check.setType(null);
        }
        if (check.getStartTime().length()==0){
            check.setStartTime(null);
        }
        if (check.getEndTime().length()==0){
            check.setEndTime(null);
        }
        List<ContractVo> list = contractVoDao.ConditionalQuery(check);
        CastPageUtil castPageUtil = new CastPageUtil(list, Integer.parseInt(nowPage));
        List dataList = castPageUtil.getCurrentDataList();
        return dataList;
    }

    @Override
    public Integer checkInfo(Check check) {
        List<ContractVo> list = contractVoDao.ConditionalQuery(check);
        CastPageUtil castPageUtil = new CastPageUtil(list, 1);
        return castPageUtil.getTotalNum();
    }

    @Override
    public Integer deleteById(Integer id) {
        Integer integer = contractDao.deleteById(id);
        return integer;
    }

    @Override
    public Integer updateById(Integer id,String paystate) {
        Integer integer = contractDao.updateContractById(id, paystate);
        return integer;
    }
}